package impl;

import java.sql.ResultSet;
import java.util.List;

import business.basic.BaseDAO;
import business.basic.BaseDaoImpl;
import DAO.TBookDAO;
import Model.TBook;

public class TBookimpl implements TBookDAO {

	BaseDAO bdao =new BaseDaoImpl();
//	public void init()
//	{
//		bdao = new BaseDaoImpl();
//	}
	@Override
	public boolean addbook(TBook book) {
		String sqlString="insert into T_Book(name,author,press,booktype,directory,introduction,bookstate) Values (?,?,?,?,?,?,?)";
		Object[] para = {book.getName(),book.getAuthor(),book.getPress(),book.getBooktype(),
				book.getIntroduction(),book.getBooktype()};
		if((Integer)bdao.executeProduce(sqlString, para)>0){
			return true;
		}else {
			return false;
		}
		
	}

	@Override
	public boolean deletebook(TBook book) {
		String sqlString="delete from T_Book where id = ?";
		Object[] para = {book.getId()};
		if((Integer)bdao.executeProduce(sqlString, para)>0){
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean updatebook(TBook book) {
		String sqlString="UPDATE T_Book SET name=?,author=?,press=?,booktype=?,directory=?,introduction=?,bookstate=? WHERE id=?";
		Object[] para = {book.getName(),book.getAuthor(),book.getPress(),book.getBooktype(),
				book.getIntroduction(),book.getBooktype(),book.getId()};
		if((Integer)bdao.executeProduce(sqlString, para)>0){
			return true;
		}else {
			return false;
	}
	}
	@Override
	public TBook selectBook(int id) {
		String sql = "select * from T_Book where id = ?";
		Object[] para ={id};
		ResultSet rs = bdao.select(sql,para);
		TBook tb=new TBook(rs);		
		return tb;
	}

	@Override
	public ResultSet selectall() {
		String sql = "select * from T_Book";
		ResultSet rs = bdao.select(sql);
		return rs;
	}
//	public static void main(String[] args) {
//		TBookimpl tBookimpl=new TBookimpl();
//		
//		System.out.println(tBookimpl.selectall());
//		
//	}
	@Override
	public List<TBook> selectBook(String like) {
		BaseDaoImpl bdao = new BaseDaoImpl();
		String sql = "select * from T_Book where name like '%"+like+"%'";
		ResultSet rs = bdao.select(sql);
		TBook tb=new TBook(rs);	
		List<TBook> list=tb.toList(rs);		
		return list;
//		String sql = "select * from T_Book where name like '%"+like+"%'";
//		ResultSet rs = bdao.select(sql);
//		TBook tb=new TBook(rs);
//		List<TBook> list=tb.toList(rs);
//		return list;
	}

}
